import mongoose from 'mongoose';
import { ApiProperty } from '@nestjs/swagger';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { Category } from './category.schema';

@Schema({
  timestamps: true,
})
export class Book extends Document {
  @ApiProperty({ description: '图书名称', example: '红楼梦' })
  @Prop({ required: true })
  name: string;

  @ApiProperty({ description: '作者', example: '曹雪芹' })
  @Prop({ required: true })
  author: string;

  @ApiProperty({
    description: '图书分类ID',
    example: 'xxxxxx',
  })
  @Prop({
    required: true,
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Category' }],
  })
  categories: Category[];

  @ApiProperty({ description: '库存或数量', example: '1' })
  @Prop({ required: true })
  number: number;

  @ApiProperty({ description: '单价', example: '49' })
  @Prop({ required: true })
  price: number;

  @ApiProperty({ description: '图书封面地址', example: '/xxx' })
  @Prop()
  image: string;

  @ApiProperty({ description: '图书简介', example: 'xxxxxx' })
  @Prop()
  description: string;

  @ApiProperty({ description: '销量', example: '0' })
  @Prop({ default: 0 })
  sales: number;
}

export type BookDocument = Book & Document;

export const BookSchema = SchemaFactory.createForClass(Book);
